package aom.example.security.service.impl;

import aom.example.security.domain.SysRoleEntity;
import aom.example.security.mapper.SysRoleMapper;
import aom.example.security.service.SysRoleService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:49
 * @since JDK 1.8
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Override
    public SysRoleEntity getSysRoleEntityByUserName(String userName) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysRoleEntityLambdaQueryWrapper.eq(SysRoleEntity::getRoleName,userName);
        return super.baseMapper.selectOne(sysRoleEntityLambdaQueryWrapper);
    }

    /**
     * 通过uid查询用户当前具有的角色
     * @param userid
     * @return
     */
    @Override
    public List<Map<String, Object>> selectRoleUserByUserId(String userid) {
        List<Map<String, Object>> mapList=this.baseMapper.selectRoleUserByUserId(userid);
        return mapList;
    }

    @Override
    public boolean insertSysUserRole(String userid, String roleid) {
        return this.baseMapper.insertSysUserRole(userid,roleid);
    }

    @Override
    public boolean insertSysRoleMenu(String roleId, String menuId) {
        return this.baseMapper.insertSysRoleMenu(roleId,menuId);
    }

    @Override
    public List<Map<String, Object>> selectUserNotRole(String uid) {
        return this.baseMapper.selectUserNotRole(uid);
    }

    @Override
    public boolean deleteSysUserRoleById(String id) {
        return this.baseMapper.deleteSysUserRoleById(id);
    }


    @Override
    public List<Map<String, Object>> selectRoleMenuList(List<String> ids) {
        System.out.println(JSON.toJSONString(ids));
        return this.baseMapper.selectMenuRoleByIds(ids);
    }

    @Override
    public Map<String, Object> selectByRoleIdMenuId(String roleId, String userId) {
        return this.baseMapper.selectMenuRole(roleId, userId);
    }

    @Override
    public IPage<Map<String, Object>> selectRoleMenu(String roleId, String keyword, Integer page, Integer size) {
        page=(page-1)*size;
        Map map=new HashMap();
        map.put("page",page);
        map.put("size",size);
        map.put("keyword",keyword);
        map.put("roleid",roleId);
        List<Map<String, Object>> maps=this.baseMapper.selectMenuRolePage(map);
        Long count=this.baseMapper.selectMenuRoleCount(map);
        IPage<Map<String, Object>> page1=new Page<>();
        page1.setRecords(maps);
        page1.setTotal(count);
        return page1;
    }

    @Override
    public boolean deleteRoleMenuById(Integer id) {
        return this.baseMapper.deleteRoleMenuById(id);
    }

    @Override
    public IPage<Map<String, Object>> pageRoleUser(String userId, String keyword, Integer pageNum, Integer pageSize) {
        pageNum=(pageNum-1)*pageSize;
        List<Map<String, Object>> maps=this.baseMapper.pageSysUserRole(userId,keyword,pageNum,pageSize);
        Long count=this.baseMapper.countSysUserRole(userId,keyword);
        IPage<Map<String, Object>> page=new Page();
        page.setRecords(maps);
        page.setTotal(count);
        return page;
    }

    @Override
    public boolean deleteSysRoleMenuByRoleId(String roleId) {
        return this.baseMapper.deleteSysRoleMenuByRoleId(roleId);
    }
}
